//
//  BindDevViewController.m
//  SIPphone
//
//  Created by wrt on 15-2-6.
//  Copyright (c) 2015年 wrtsoft. All rights reserved.
//

#import "BindDevViewController.h"
#import "SVProgressHUD.h"
#import "AddExtensionViewController.h"

@interface BindDevViewController ()
{
    NSDictionary *selectDic;
}
@end

@implementation BindDevViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NetPostClass shareNetPostClass] setDelegate:self];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *tempUserName = [defaults valueForKey:@"SAVEUSERNAME"];
    NSString *tempPassWord = [defaults valueForKey:@"SAVEPASSWORD"];
    //查询绑定列表
    [[NetPostClass shareNetPostClass] postBindingListWithUserName:tempUserName andPassWord:tempPassWord];
    [SVProgressHUD showWithStatus:NSLocalizedString(@"loading list", @"正在查询绑定列表 Loading list")];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = NSLocalizedString(@"bound list", @"已绑定列表 Bound list");
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"add", @"添加 add") style:UIBarButtonItemStylePlain target:self action:@selector(addButtonPressed)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
}

- (void)addButtonPressed
{
    AddExtensionViewController *addExtensionVC = [[AddExtensionViewController alloc] init];
    [self.navigationController pushViewController:addExtensionVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark – UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //static NSString *cellIdentifier = @"MenuCell";
    UITableViewCell *cell = nil;
    static NSString *objectCell_ID = @"ListCell";
    cell = [tableView dequeueReusableCellWithIdentifier:objectCell_ID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:objectCell_ID];
    }
    
    NSDictionary *devInfoDic = self.listArray[indexPath.row];
    cell.textLabel.text = devInfoDic[@"deviceName"];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.text = devInfoDic[@"deviceCode"];
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    return cell;
}




#pragma mark – UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectDic = self.listArray[indexPath.row];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"unbind extension", @"是否解除对该分机的绑定 Unbind extension") delegate:self cancelButtonTitle:NSLocalizedString(@"cancel", @"取消 cancel") otherButtonTitles:NSLocalizedString(@"sure", @"确定 Sure"), nil];
    alert.tag = 101;
    [alert show];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - UIAlertView的代理操作
//UIAlertView的代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 101)
    {
        if (buttonIndex == 0)
        {
            
        }
        else if (buttonIndex == 1)
        {
            //让其输入分机密码
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"please enter the password for the extension", @"请输入该分机的密码 Please enter the password for the extension") delegate:self cancelButtonTitle:NSLocalizedString(@"cancel", @"取消 cancel") otherButtonTitles:NSLocalizedString(@"sure", @"确定 Sure"), nil];
            alert.tag = 102;
            [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
            UITextField *textField=[alert textFieldAtIndex:0];
            [textField setSecureTextEntry:YES];
            [alert show];
            
        }
    }
    else if (alertView.tag == 102)
    {
        if (buttonIndex == 0)
        {
            
        }
        else if (buttonIndex == 1)
        {
            UITextField *textField=[alertView textFieldAtIndex:0];
            if (textField.text.length >0)
            {
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                NSString *tempUserName = [defaults valueForKey:@"SAVEUSERNAME"];
                NSString *tempPassWord = [defaults valueForKey:@"SAVEPASSWORD"];
                //解除绑定
                [[NetPostClass shareNetPostClass] postUnBindExtensionWithUserName:tempUserName andPassWord:tempPassWord andDeviceCode:selectDic[@"deviceCode"] andDevicePassword:textField.text];
            }
            else
            {
                [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"passwords can not be empty", @"密码不能为空 Passwords can not be empty")];
            }
        }
    }
    
}

#pragma mark - NetPostDelegateMethod
/**
 *  post数据成功回调
 *
 *  @param comeBackDic 返回来的字典数据
 *  @param tagNumber   post的tag标识
 */
- (void)postSuccessfulWithString:(NSDictionary *)comeBackDic withTag:(int)tagNumber
{
    if (tagNumber == 9)
    {
        [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"complete list of queries", @"查询列表完成 Complete list of queries")];
        //post成功返回数据
        NSDictionary *tempDic = comeBackDic;
        self.listArray = tempDic[@"getBind"];
        [self.tableview reloadData];
    }
    else if (tagNumber == 11)
    {
        [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"deleted successfully", @"删除成功 Deleted successfully")];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *tempUserName = [defaults valueForKey:@"SAVEUSERNAME"];
        NSString *tempPassWord = [defaults valueForKey:@"SAVEPASSWORD"];
        //查询绑定列表
        [[NetPostClass shareNetPostClass] postBindingListWithUserName:tempUserName andPassWord:tempPassWord];
    }
}

/**
 *  post失败返回数据
 *
 *  @param faileString 失败的原因
 *  @param tagNumber   post的tag标识
 */
- (void)postFailedWithString:(NSString *)faileString withTag:(int)tagNumber
{
    //post失败返回的失败原因
    dispatch_async(dispatch_get_main_queue(),^{
        if (tagNumber == 9)
        {
            [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"query list failed", @"查询列表失败 Query list failed")];
        }
        else if (tagNumber == 11)
        {
            [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"unbind failure", @"解除绑定失败 Unbind failure")];
        }
        
    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
